﻿using System.Xml.Serialization;
using System;

namespace ServerFramework.Net.Msg
{
    
    ///协议模型
    /// 消息包长度  4 个字节 类型 int
    /// 消息MsgId  4 个字节  类型 int
    /// UserId 指用来保存session的用户id 4 个字节
    /// _Status 预留字段 4个字节
    [Serializable]
    public class MsgBase
    {
        [XmlIgnore]//json 忽略
        [NonSerialized]
        public const int MSG_HEAD_LENTH = 16;
        [XmlIgnore]//json 忽略
        [NonSerialized]
        protected bool _compress = false;  //是否压缩消息
        [XmlIgnore]//json 忽略
        [NonSerialized]
        public int _lenth;           //消息长度
        [XmlIgnore]//json 忽略
        [NonSerialized]
        public int _msgId;           //消息id
        [XmlIgnore]
        [NonSerialized]
        public int _userId;          //用户id
        [XmlIgnore]
        [NonSerialized]
        public int _status;          //预留用作错误代码

        public MsgBase()
        {
            WriteMsgId();
        }

        protected virtual void WriteMsgId()
        {
            _msgId = 0;
        }

        public virtual byte[] Pack()
        {
            MsgSn sn = new MsgSn();
            //为长度占位
            sn.Write(MSG_HEAD_LENTH);
            sn.Write(_msgId);
            sn.Write(_userId);
            sn.Write(_status);
            _lenth = MSG_HEAD_LENTH;
            return sn.GetMsg();
        }
        public virtual void Unpack(MsgPacket packet)
        {
            _lenth = packet.lenth;
            _msgId = packet.msgId;
            _userId = packet.userId;
            _status = packet.status;
        }
    }
}
